package hydrap2p.hubmanager;

import java.io.IOException;
import java.net.BindException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import hydrap2p.helper.ServiceConnector;
import hydrap2p.helper.ThriftServerThread;
import hydrap2p.helper.ThriftUtils;
import hydrap2p.hub.HubManager;
import hydrap2p.hub.HubStatus;
import hydrap2p.modulemanager.ModuleControl;
import hydrap2p.servicemanager.NameTaken;
import hydrap2p.servicemanager.ServiceManager;

public class HubManagerMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {	
		try {
			ThriftServerThread thread = ThriftUtils.initThriftServer(new HubManager.Processor(new Server()), "HubManager");
			thread.start();
		} catch (BindException e) {
			System.err.println("Could not start listening");
		} catch (IOException e) {
			System.err.println("Could not connect to Manager");
		} catch (NameTaken e) {
			System.err.println("Another HubManager is already running");
		}
		try {
			ThriftServerThread thread = ThriftUtils.initThriftServer(new ModuleControl.Processor(new HubManagerCTL()), "HubManagerCTL");
			thread.start();
		} catch (BindException e) {
			System.err.println("Could not start listening");
		} catch (IOException e) {
			System.err.println("Could not connect to Manager");
		} catch (NameTaken e) {
			System.err.println("Another HubManager is already running");
		}
	}

	public static void shutdown() {
		ServiceManager.Iface manager=ServiceConnector.getServiceManager();
		try {
			manager.delServiceLocation("HubManager");
			manager.delServiceLocation("HubManagerCTL");
		} catch (Exception e)
		{}
		System.exit(0);
	}
}
